package com.imc.platform.base.appointment.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.api.order.pojo.LessonSchedule;
import com.imc.core.config.feign.MyPageImpl;
import com.imc.platform.base.appointment.dao.IAppointmentMapper;
import com.imc.platform.base.appointment.pojo.Appointment;
import com.imc.platform.base.org.pojo.Org;
import com.imc.platform.base.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 类描述：预约信息服务类
 * 开发者：YT
 * 创建日期：2018/10/12 23:01
 */
@Transactional
@Slf4j
@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    IAppointmentMapper iAppointmentMapper;

    /*
     * YT  2018/10/17 23:25
     * 方法描述：根据条件查询预约信息
     */
    public PageInfo <Appointment> queryAppointmentByMap(int pageNumber, int pageSize, Map <String, String> appointmentMap) {
        PageHelper.startPage(pageNumber, pageSize, true);
        PageInfo info = new PageInfo <Appointment>(iAppointmentMapper.queryAppointmentByMap(appointmentMap));
        return info;
    }

    /*
     * YT  2018/10/24 18:59
     * 方法描述：根据用户查询对应的预约信息，同一个用户存在多个预约信息
     */
    @Override
    public List <Map <String, Object>> getAppointmentListNoPage(Appointment appointment) {
        val data = iAppointmentMapper.getAppointmentList(appointment);
        if (data == null || data.isEmpty()) return new ArrayList <>();
        return data;
    }

    /*
     * YT  2018/10/24 18:59
     * 方法描述：根据用户查询对应的预约信息，同一个用户存在多个预约信息
     */
    @Override
    public MyPageImpl getAppointmentListPage(Appointment appointment, Pageable pageable) {
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        val data = iAppointmentMapper.getAppointmentList(appointment);
        PageInfo info = new PageInfo <>(data);
        return new MyPageImpl(info.getList(), info.isIsLastPage());
    }

    /*
     * YT  2018/10/27 22:01
     * 方法描述：根据课程ID查询课时列表
     */
    @Override
    public List <LessonSchedule> queryLessonSchedulerByLessonId(String lessonId, String userId) {
        try {
            val data = iAppointmentMapper.queryLessonSchedulerByLessonId(lessonId, userId);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /*
     * YT  2018/10/17 23:14
     * 方法描述：新增预约信息
     */
    @Override
    @CacheEvict(value = "appointment", allEntries = true)
    public int addAppointment(Appointment appointment) {
        int count = iAppointmentMapper.addAppointment(appointment);
        return count;
    }

    /*
     * YT  2018/10/17 23:16
     * 方法描述：查看预约信息
     */
    @Override
    public Appointment queryAppointmentInfoById(String id) {
        Appointment appointmentInfo = iAppointmentMapper.queryAppointmentInfoById(id);
        return appointmentInfo;
    }

    /**
     * QY 2018/9/3 10:16
     * 方法描述：更改状态
     **/
    @Override
    public int updateState(String state, String id) {
        if ("false".equals(state)) {
            state = "0";
        } else if ("true".equals(state)) {
            state = "1";
        }
        return iAppointmentMapper.updateStateDeal(state, id);
    }

    /*
     * YT  2018/10/25 13:08
     * 方法描述：查询组织机构ID 信息
     */
    @Override
    public Org queryOrgIdByLessonId(String lessonId) {
        return iAppointmentMapper.queryOrgIdByLessonId(lessonId);
    }

    /*
     * YT  2018/10/27 22:01
     * 方法描述：根据组织机构ID查询用户信息
     */
    @Override
    public String queryUserByOrgId(String orgId) {
        List <String> list = iAppointmentMapper.queryUserIdByOrgId(orgId);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /*
     * YT  2018/10/27 22:00
     * 方法描述：查询预约信息是否存在
     */
    @Override
    public List <Appointment> queryAppointmentExists(Appointment appointment) {
        return iAppointmentMapper.queryAppointmentExists(appointment);
    }

}